<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="150px"
    >
      <el-form-item label="定点医药机构名称" prop="fixmedinsName">
        <el-input
          v-model="queryParams.fixmedinsName"
          placeholder="请输入定点医药机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pr:basics:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pr:basics:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pr:basics:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pr:basics:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="basicsList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column
        label="定点医药机构编号"
        align="center"
        prop="fixmedinsCode"
      />
      <el-table-column
        label="定点医药机构名称"
        align="center"
        prop="fixmedinsName"
      />
      <el-table-column label="推送类型" align="center" prop="sourceType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_push_types"
            :value="scope.row.sourceType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="医院类型"
        align="center"
        prop="hospitalSourceType"
      />
      <el-table-column label="处方类型" align="center" prop="prescriptionType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_prescription_types"
            :value="scope.row.prescriptionType"
          />
        </template>
      </el-table-column>
      <el-table-column label="推送状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_push_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handlePush(scope.row)" 
            v-if="scope.row.status == 2"
            >推送</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="lookDetail(scope.row)"
            >查看</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pr:basics:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pr:basics:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改处方流转-基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="定点医药机构编号" prop="fixmedinsCode">
          <el-input
            v-model="form.fixmedinsCode"
            placeholder="请输入定点医药机构编号"
          />
        </el-form-item>
        <el-form-item label="定点医药机构名称" prop="fixmedinsName">
          <el-input
            v-model="form.fixmedinsName"
            placeholder="请输入定点医药机构名称"
          />
        </el-form-item>
        <el-form-item label="推送类型" prop="sourceType">
          <el-select v-model="form.sourceType" placeholder="请选择推送类型">
            <el-option
              v-for="dict in dict.type.yy_push_types"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处方类型" prop="prescriptionType">
          <el-select
            v-model="form.prescriptionType"
            placeholder="请选择处方类型"
          >
            <el-option
              v-for="dict in dict.type.yy_prescription_types"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.yy_push_status"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="详情"
      :visible.sync="openDetail"
      width="1200px"
      append-to-body
    >
    
      <el-card class="box-card">
        <div class="detail-wrap">
        <!-- 基础信息 -->
        <basicsInfo ref="info" style="margin-bottom: 20px;"></basicsInfo>
        <!-- 诊断信息 -->
        <diagnoseInfo ref="diagnose" style="margin-bottom: 20px;"></diagnoseInfo>
        <!-- 医嘱信息 -->
        <doctoAdvice ref="docto" style="margin-bottom: 20px;"></doctoAdvice>
        <!-- 处方扩展 -->
        <extendInfo ref="extend" style="margin-bottom: 20px;"></extendInfo>
        <!-- 处方信息 -->
        <prescriptionInfo ref="prescription"></prescriptionInfo>
      </div>
      </el-card>
      <!-- <el-tabs type="border-card">
        <el-tab-pane label="基础信息"> 
          <basicsInfo ref="info"></basicsInfo> 
        </el-tab-pane>
        <el-tab-pane label="医嘱信息"
          ><doctoAdvice ref="docto"></doctoAdvice
        ></el-tab-pane>
        <el-tab-pane label="处方信息"
          ><prescriptionInfo ref="prescription"></prescriptionInfo
        ></el-tab-pane>
        <el-tab-pane label="诊断信息"
          ><diagnoseInfo ref="diagnose"></diagnoseInfo
        ></el-tab-pane>
        <el-tab-pane label="处方扩展"
          ><extendInfo ref="extend"></extendInfo
        ></el-tab-pane> 
      </el-tabs> -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelDetail">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBasics,
  getBasics,
  delBasics,
  addBasics,
  updateBasics,
  manualPush
} from "@/api/pr/basics";
import basicsInfo from "./components/basicsInfo";
import diagnoseInfo from "./components/diagnoseInfo";
import doctoAdvice from "./components/doctoAdvice"; 
import extendInfo from "./components/extendInfo";
import prescriptionInfo from "./components/prescriptionInfo";
export default {
  components: {
    basicsInfo,
    diagnoseInfo,
    doctoAdvice,
    extendInfo, 
    prescriptionInfo,
  },
  name: "Basics",
  dicts: ["yy_prescription_types", "yy_push_types", "yy_push_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方流转-基础信息表格数据
      basicsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fixmedinsName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fixmedinsCode: [
          {
            required: true,
            message: "定点医药机构编号不能为空",
            trigger: "blur",
          },
        ],
        fixmedinsName: [
          {
            required: true,
            message: "定点医药机构名称不能为空",
            trigger: "blur",
          },
        ],
        sourceType: [
          { required: true, message: "推送类型不能为空", trigger: "change" },
        ],
        hospitalSourceType: [
          { required: true, message: "医院类型不能为空", trigger: "change" },
        ],
        prescriptionType: [
          { required: true, message: "处方类型不能为空", trigger: "change" },
        ],
      },
      openDetail: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePush(row) {
      let param = {
        id: row.id,
        fixmedinsCode: row.fixmedinsCode,
      };  
      manualPush(param)
        .then((response) => { 
          this.$modal.msgSuccess("推送成功");
          this.getList();
        })
        .cancel(() => { 
        });
    },
    // 关闭弹窗
    cancelDetail() {
      this.openDetail = false;
    },
    // 查看详情
    lookDetail(row) {
      this.openDetail = true;
      this.$nextTick(() => {
        this.$refs.info.getdDetail(row.id);
        this.$refs.docto.getdDetail(row.id);
        this.$refs.prescription.getdDetail(row.id);
        this.$refs.diagnose.getdDetail(row.id);
        this.$refs.extend.getdDetail(row.id);
      });

      // this.$router.push({
      //   path: `/basicsIndex/detail/${row.id}`
      // });
    },
    /** 查询处方流转-基础信息列表 */
    getList() {
      this.loading = true;
      listBasics(this.queryParams).then((response) => {
        this.basicsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fixmedinsCode: null,
        fixmedinsName: null,
        sourceType: null,
        hospitalSourceType: null,
        prescriptionType: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方流转-基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBasics(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方流转-基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBasics(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasics(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除处方流转-基础信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBasics(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pr/basics/export",
        {
          ...this.queryParams,
        },
        `basics_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.detail-wrap{
  overflow-y: scroll;
  height: 60vh; 
}
</style>
